package com.mstore.dao;

import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.mstore.pojo.Consume;
import com.mstore.pojo.ExamineSetting;

public class ExamineSettingDao extends SuperDao {

	/**
	 * * 通过企业id查询自动审核规则信息 
	 * * @param eaId 
	 * * @return
	 */
	public Object getExamineSettingByCondition(ExamineSetting setting) {
		String separate = " where ";
		StringBuilder sql = new StringBuilder();
		sql.append("from ExamineSetting");

		Long id = setting.getId();
		if (id != null) {
			sql.append(separate);
			sql.append("id = ");
			sql.append(id);
			separate = " and ";
		}

		Long eaId = setting.getEaId();
		if (eaId != null) {
			sql.append(separate);
			sql.append("eaId = ");
			sql.append(eaId);
			separate = " and ";
		}

		List<Object> list = find(sql.toString());
		if (list.isEmpty()) {
			return null;
		}

		return list.get(0);
	}

	/**
	 * * 保存新自动审核规则
	 * * @param setting
	 */
	public void addExamineSetting(ExamineSetting setting) {
		save(setting);
	}

	/**
	 * * 更新自动审核规则 
	 * * @param setting
	 */
	public void updateExamineSetting(ExamineSetting setting) {
		String separate = " set ";
		StringBuilder sql = new StringBuilder();
		sql.append("update ubcenter.examine_setting");

		Double dailyMoneyLimit = setting.getDailyMoneyLimit();
		if (dailyMoneyLimit != null) {
			sql.append(separate);
			sql.append("daily_money_limit = ");
			sql.append(dailyMoneyLimit);
			separate = ", ";
		}

		String examineBegintime = setting.getExamineBegintime();
		if (StringUtils.isNotBlank(examineBegintime)) {
			sql.append(separate);
			sql.append("examine_begintime = '");
			sql.append(examineBegintime);
			sql.append("'");
			separate = ", ";
		}

		Integer examineInterval = setting.getExamineInterval();
		if (examineInterval != null) {
			sql.append(separate);
			sql.append("examine_interval = ");
			sql.append(examineInterval);
			separate = ", ";
		}

		Integer examineStart = setting.getExamineStart();
		if (examineStart != null) {
			sql.append(separate);
			sql.append("examine_start = ");
			sql.append(examineStart);
			separate = ", ";
		}

		sql.append(" where ex_id = ");
		sql.append(setting.getId());

		executeUpdate(sql.toString());
	}

	/**
	 * * 检查是否已设置过消费审核规则 
	 * * @param eaId 
	 * * @return
	 */
	public boolean isSetting(Long eaId) {
		String sql = "select count(id) from ExamineSetting where eaId = "
				+ eaId;
		List<Object> list = find(sql);
		if (list.isEmpty()) {
			return false;
		}

		int count = ((Long) list.get(0)).intValue();

		return count > 0 ? true : false;
	}

	/**
	 * * 删除消费审核规则 
	 * * @param setting
	 */
	public void delExamineSetting(ExamineSetting setting) {
		delete(setting);
	}
	
	/**
	 * * 根据条件查询消费记录
	 * * @return
	 */
	public List<Object> getConsumesByCondition(Consume consume) {
		StringBuilder sql = new StringBuilder();
		sql.append("from Consume");
		sql.append(createConsumeSqlCondition(consume));
		sql.append(" order by createTime desc");
		
		return find(sql.toString());
	}
	
	/**
	 * * 根据条件查询消费记录
	 * * 分页
	 * * @return
	 */
	public List<Object> getPageConsumesByCondition(int index, Consume consume) {
		StringBuilder sql = new StringBuilder();
		sql.append("from Consume");
		sql.append(createConsumeSqlCondition(consume));
		sql.append(" order by createTime desc");
		
		return findPage(index, sql.toString());
	}
	
	/**
	 * * 根据条件查询消费记录总数量
	 * * @return
	 */
	public int getCountConsumesByCondition(Consume consume) {
		StringBuilder sql = new StringBuilder();
		sql.append("select count(id) from Consume");
		sql.append(createConsumeSqlCondition(consume));
		
		List<Object> list = find(sql.toString());
		if (list.isEmpty()) {
			return 0;
		}
		return ((Long)list.get(0)).intValue();
	}
	
	/**
	 * * 根据条件查询消费记录的总金额
	 * * @return
	 */
	public double getSumConsumesByCondition(Consume consume) {
		StringBuilder sql = new StringBuilder();
		sql.append("select sum(money) from Consume");
		sql.append(createConsumeSqlCondition(consume));
		
		List<Object> list = find(sql.toString());
		if (list.isEmpty() || list.get(0) == null) {
			return 0;
		}
		return ((Double)list.get(0)).doubleValue();
	}
	
	/**
	 * * 修改消费记录审核标示
	 * * @param consume
	 */
	public void modifyConsumeExamineFlag(Consume consume) {
		StringBuilder sql = new StringBuilder();
		sql.append("update ubcenter.log_consume");
		
		Integer examineFlag = consume.getExamineFlag();
		if (examineFlag != null) {
			sql.append(" set examine_flag = ");
			sql.append(examineFlag);
		}
		
		Long id = consume.getId();
		if (id != null) {
			sql.append(" where id = ");
			sql.append(id);
		}
		
		executeUpdate(sql.toString());
	}
	
	/**
	 * * 根据条件组装消费记录的sql条件语句
	 * * @param consume
	 * * @return
	 */
	private String createConsumeSqlCondition(Consume consume) {
		String separate = " where ";
		StringBuilder sql = new StringBuilder();
		
		// 主键
		Long id = consume.getId();
		if (id != null) {
			sql.append(separate);
			sql.append("id = ");
			sql.append(id);
			separate = " and ";
		}
		
		// 企业id
		Long eaId = consume.getEaId();
		if (eaId != null) {
			sql.append(separate);
			sql.append("eaId = ");
			sql.append(eaId);
			separate = " and ";
		}
		
		// 店铺id
		Long shopId = consume.getShopId();
		if (shopId != null && shopId != 0) {
			sql.append(separate);
			sql.append("shopId = ");
			sql.append(shopId);
			separate = " and ";
		}
		
		// 操作员
		Long userId = consume.getUserId();
		if (userId != null) {
			sql.append(separate);
			sql.append("userId = ");
			sql.append(userId);
			separate = " and ";
		}
		
		// 会员id
		Long memberId = consume.getMemberId();
		if (memberId != null) {
			sql.append(separate);
			sql.append("memberId = ");
			sql.append(memberId);
			separate = " and ";
		}
		
		// 会员手机号
		String memberMobile = consume.getMemberMobile();
		if (StringUtils.isNotBlank(memberMobile)) {
			sql.append(separate);
			sql.append("memberMobile = '");
			sql.append(memberMobile);
			sql.append("'");
			separate = " and ";
		}
		
		// 审核标示
		Integer examineFlag = consume.getExamineFlag();
		if (examineFlag != null && examineFlag != -1) {
			sql.append(separate);
			sql.append("examineFlag = ");
			sql.append(examineFlag);
			separate = " and ";
		}
		
		// 小票号
		String smallBanks = consume.getSmallBanks();
		if (StringUtils.isNotBlank(smallBanks)) {
			sql.append(separate);
			sql.append("smallBanks = '");
			sql.append(smallBanks);
			sql.append("'");
			separate = " and ";
		}
		
		// 时间范围
		String beginTime = consume.getBeginTime();
		String endTime = consume.getEndTime();
		if (StringUtils.isNotBlank(beginTime) && StringUtils.isBlank(endTime)) {
			sql.append(separate);
			sql.append("createTime >= '");
			sql.append(beginTime);
			sql.append("'");
			separate = " and ";
		}else if (StringUtils.isBlank(beginTime) && StringUtils.isNotBlank(endTime)) {
			sql.append(separate);
			sql.append("createTime <= '");
			sql.append(endTime);
			sql.append("'");
			separate = " and ";
		}else if (StringUtils.isNotBlank(beginTime) && StringUtils.isNotBlank(endTime)) {
			sql.append(separate);
			sql.append("(createTime between '");
			sql.append(beginTime);
			sql.append("' and '");
			sql.append(endTime);
			sql.append("')");
			separate = " and ";
		}
		
		// 金额范围
		Double minMoney = consume.getMinMoney();
		Double maxMoney = consume.getMaxMoney();
		if (minMoney != null && maxMoney == null) {
			sql.append(separate);
			sql.append("money >= ");
			sql.append(minMoney);
			separate = " and ";
		}else if (minMoney == null && maxMoney != null) {
			sql.append(separate);
			sql.append("money <= ");
			sql.append(maxMoney);
			separate = " and ";
		}else if (minMoney != null && maxMoney != null) {
			sql.append(separate);
			sql.append("(money between ");
			sql.append(minMoney);
			sql.append(" and ");
			sql.append(maxMoney);
			sql.append(")");
			separate = " and ";
		}
		
		return sql.toString();
	}

}
